package com.globefrog.example.consumer;

import java.sql.Driver;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.jdbc.datasource.embedded.ConnectionProperties;
import org.springframework.jdbc.datasource.embedded.DataSourceFactory;

/**
 * 不引入jdbc,自定义驱动数据源链接工厂
 * @author : Globe-Frog
 * @since : Create in 2021/7/12
 */
public class CustomDriverDataSourceFactory implements DataSourceFactory {

  private final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();

  CustomDriverDataSourceFactory() {
  }

  public ConnectionProperties getConnectionProperties() {
    return new ConnectionProperties() {
      public void setDriverClass(Class<? extends Driver> driverClass) {
        CustomDriverDataSourceFactory.this.dataSource.setDriverClass(driverClass);
      }

      public void setUrl(String url) {
        CustomDriverDataSourceFactory.this.dataSource.setUrl(url);
      }

      public void setUsername(String username) {
        CustomDriverDataSourceFactory.this.dataSource.setUsername(username);
      }

      public void setPassword(String password) {
        CustomDriverDataSourceFactory.this.dataSource.setPassword(password);
      }
    };
  }

  public DataSource getDataSource() {
    return this.dataSource;
  }
}
